package com.iris.live.services.services.impl;

import com.iris.live.services.common.JDBCBaseUtils;
import com.iris.live.services.common.config.Constants;
import com.iris.live.services.common.enumpack.CompanyGroupEnum;
import com.iris.live.services.data.repositories.CommonSearchRepository;
import com.iris.live.services.services.CommonSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by gengshuai on 26/12/2017.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
@Transactional
public class CommonSearchServiceImpl implements CommonSearchService {
    private static final Logger logger = LoggerFactory.getLogger(CommonSearchServiceImpl.class);

    @Autowired
    private CommonSearchRepository commonSearchRepository;

    /**
     * 获取用户名所属大区LIST
     *
     * @param comm
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> listAreas(CommonSearchRequest comm) {

        Map<String,Object> map = new HashMap<>();
        map.put("data", commonSearchRepository.getAreaLists(comm.frUserName,
                comm.frAuthority,
                comm.isable));

        return map;

        /*Map<String,Object> map = new HashMap<>();

        CompanyGroupEnum companyGroupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (companyGroupEnum) {
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getAreaLists(comm.frUserName,
                        comm.frAuthority,
                        comm.isable));
                break;
            case DCH:
            case LVD:
                map.put("data", commonSearchRepository.getAreaLists(comm.frUserName,
                        comm.frAuthority,
                        comm.isable));
                break;
            default:
                break;
        }
        return map;*/
    }

    /**
     * PMA API
     *
     * @param comm
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> listPmas(CommonSearchRequest comm) {

        Map<String,Object> map = new HashMap<>();
        CompanyGroupEnum companyGroupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);

        if(Constants.VERSION_LSH_ONE == companyGroupEnum.getVersion()) {
            map.put("data", commonSearchRepository.getPmaLists(comm.frUserName,
                    comm.frAuthority,
                    comm.selRegionCode,
                    comm.isable));
        }else if (Constants.VERSION_DCH_TWO == companyGroupEnum.getVersion()) {
            map.put("data", JDBCBaseUtils.runSelectSql("SELECT 'all' 大区_EN,'all' CTA_EN"));
        }


        /*switch (companyGroupEnum) {
            //利星行版本
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getPmaLists(comm.frUserName,
                        comm.frAuthority,
                        comm.selRegionCode,
                        comm.isable));
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", JDBCBaseUtils.runSelectSql("SELECT 'all' 大区_EN,'all' CTA_EN"));
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 经销商 API
     *
     * @param comm
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> listDealers(CommonSearchRequest comm) {

        Map<String,Object> map = new HashMap<>();
        CompanyGroupEnum companyGroupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);

        if(Constants.VERSION_LSH_ONE == companyGroupEnum.getVersion()) {

            map.put("data", commonSearchRepository.getDealerLists(comm.selRegionCode, comm.selPmaCode));
        }else if (Constants.VERSION_DCH_TWO == companyGroupEnum.getVersion()) {

            map.put("data", commonSearchRepository.getDealerLists2(comm.selRegionCode));
        }

        /*switch (companyGroupEnum) {
            //利星行版本
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getDealerLists(comm.selRegionCode, comm.selPmaCode));
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", commonSearchRepository.getDealerLists2(comm.selRegionCode));
                break;

            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取活动类型API（视图->API接口）
     * @param comm
     * @return Map
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getActivityTypes(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();

        map.put("data", commonSearchRepository.getActivityTypes());

        /*CompanyGroupEnum groupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (groupEnum){
            //利星行版本
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getActivityTypes());
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", commonSearchRepository.getActivityTypes());
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取主导与发起PI（视图->API接口）
     * @param comm
     * @return Map
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getLeadingAndLaunches(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();

        map.put("data", commonSearchRepository.getLeadingAndLaunches());
        /*CompanyGroupEnum groupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (groupEnum){
            //利星行版本
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getLeadingAndLaunches());
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", commonSearchRepository.getLeadingAndLaunches());
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取车系API（视图->API接口）
     * @param comm
     * @return Map
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getCarSeries(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();
        map.put("data", commonSearchRepository.getCarSeries(comm.selBrand));
        /*CompanyGroupEnum groupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (groupEnum){
            //利星行版本
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getCarSeries(comm.selBrand));
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", commonSearchRepository.getCarSeries(comm.selBrand));
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取车型API（视图->API接口）
     *
     * @param comm
     * @return Map
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getCarModes(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();

        map.put("data", commonSearchRepository.getCarModes(comm.selBrand,comm.selMotorcycleType));
        /*CompanyGroupEnum groupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (groupEnum){
            //利星行版本
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getCarModes(comm.selBrand,comm.selMotorcycleType));
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", commonSearchRepository.getCarModes(comm.selBrand,comm.selMotorcycleType));
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取金融贷款类型API（视图->API接口）
     * @param comm
     * @return Map
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getFinanceLoanTypes(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();
        map.put("data", commonSearchRepository.getFinanceLoanTypes());

        /*CompanyGroupEnum groupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (groupEnum){
            //利星行版本
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getFinanceLoanTypes());
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", commonSearchRepository.getFinanceLoanTypes());
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取品牌API（视图->API接口）
     * @param comm
     * @return Map
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getBrands(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();
        CompanyGroupEnum companyGroupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);

        if(Constants.VERSION_LSH_ONE == companyGroupEnum.getVersion()) {

            String sql =
                    " select 'all' 品牌  \n" +
                            "union  (  select distinct brand 品牌 from vw_group_report_vehicle_brand  )";
            map.put("data", commonSearchRepository.getBrands(sql));
        }else if (Constants.VERSION_DCH_TWO == companyGroupEnum.getVersion()) {

            String sql2 =
                    " SELECT 'all' 品牌 FROM DUAL\n" +
                            " UNION\n" +
                            " SELECT DISTINCT 品牌 FROM  `lsh车型` \n" +
                            " WHERE length(品牌) <> 0\n" +
                            " ORDER BY 品牌 ";
            map.put("data", commonSearchRepository.getBrands(sql2));
        }

        /*switch (companyGroupEnum){
            //利星行版本
            case LSH:
            case LXH:
                String sql =
                        " select 'all' 品牌  \n" +
                                "union  (  select distinct brand 品牌 from vw_group_report_vehicle_brand  )";
                map.put("data", commonSearchRepository.getBrands(sql));
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                String sql2 =
                        " SELECT 'all' 品牌 FROM DUAL\n" +
                                " UNION\n" +
                                " SELECT DISTINCT 品牌 FROM  `lsh车型` \n" +
                                " WHERE length(品牌) <> 0\n" +
                                " ORDER BY 品牌 ";
                map.put("data", commonSearchRepository.getBrands(sql2));
                break;
            default:
                break;
        }*/

        return map;
    }

    /**
     * 获取品牌合计API（视图->API接口）
     * @param comm
     * @return Map
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getBrandTotals(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();
        CompanyGroupEnum companyGroupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);

        if(Constants.VERSION_LSH_ONE == companyGroupEnum.getVersion()) {

            String case1Sql = " select distinct brand 品牌 from vw_group_report_vehicle_brand ";
            map.put("data", commonSearchRepository.getBrandTotals(case1Sql));
        }else if (Constants.VERSION_DCH_TWO == companyGroupEnum.getVersion()) {

            String case2Sql = "SELECT DISTINCT 品牌 FROM `lsh车型` WHERE 品牌 is not null AND length(品牌) <> 0 ";
            map.put("data", commonSearchRepository.getBrandTotals(case2Sql));
        }


        /*switch (companyGroupEnum){
            //利星行版本
            case LSH:
            case LXH:
                String case1Sql = " select distinct brand 品牌 from vw_group_report_vehicle_brand ";
                map.put("data", commonSearchRepository.getBrandTotals(case1Sql));
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                String case2Sql = "SELECT DISTINCT 品牌 FROM `lsh车型` WHERE 品牌 is not null AND length(品牌) <> 0 ";
                map.put("data", commonSearchRepository.getBrandTotals(case2Sql));
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取渠道API（视图->API接口）
     * @param comm
     * @return Map
     */
    @Override
    public Map<String, Object> getChannels(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();

        map.put("data", commonSearchRepository.getChannels());

        /*CompanyGroupEnum groupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (groupEnum){
            //利星行版本
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getChannels());
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", commonSearchRepository.getChannels());
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取金融机构
     * @param comm
     * @return List
     */
    @Override
    public Map<String,Object> getFinancialInstitutions(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();

        map.put("data",commonSearchRepository.getFinancialInstitutions());

        /*CompanyGroupEnum groupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (groupEnum){
            //利星行版本
            case LSH:
            case LXH:
                map.put("data",commonSearchRepository.getFinancialInstitutions());
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data",commonSearchRepository.getFinancialInstitutions());
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取调研类型
     * @param comm
     * @return List
     */
    @Override
    public Map<String,Object> getResearchTypes(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();

        map.put("data",commonSearchRepository.getResearchTypes());

        /*CompanyGroupEnum groupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (groupEnum){
            //利星行版本
            case LSH:
            case LXH:
                map.put("data",commonSearchRepository.getResearchTypes());
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data",commonSearchRepository.getResearchTypes());
                break;
            default:
                break;
        }*/
        return map;
    }

    /**
     * 获取问卷版本号
     * @param comm
     * @return List
     */
    @Override
    public Map<String,Object> getQuestionVersion(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();

        map.put("data", commonSearchRepository.getQuestionVersion(comm.stage));

        /*CompanyGroupEnum groupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);
        switch (groupEnum){
            //利星行版本
            case LSH:
            case LXH:
                map.put("data", commonSearchRepository.getQuestionVersion(comm.stage));
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", commonSearchRepository.getQuestionVersion(comm.stage));
                break;
            default:
                break;
        }*/
        return map;
    }

    @Override
    public Map getSalesConsultants(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();
        CompanyGroupEnum companyGroupEnum = CompanyGroupEnum.getCompanyGroupEnum(comm.groupId);

        if (Constants.VERSION_DCH_TWO == companyGroupEnum.getVersion()) {

            map.put("data", commonSearchRepository.getSalesConsultants(comm.frUserName));
        }

        /*switch (companyGroupEnum){
            //利星行版本
            case LSH:
            case LXH:
                break;
            //基础版本（没有PMA）
            case DCH:
            case LVD:
            case JFU:
            case HHTD:
                map.put("data", commonSearchRepository.getSalesConsultants(comm.frUserName));
                break;
            default:
                break;
        }*/
        return map;
    }

    @Override
    public Map<String, Object> getPermitByUserName(CommonSearchRequest comm) {
        Map<String,Object> map = new HashMap<>();

        map.put("data", commonSearchRepository.getPermitByUserName(comm.frUserName));
        return map;
    }
}